home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ELECTRON / PCB_DESI / 1540.ZIP / PCBCA110.ZIP / WORK.C < prev   
C/C++ Source or Header  |  1990-11-18  |  3KB  |  142 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #ifndef VMS
  5. #include <malloc.h>
  6. #endif
  7.  
  8. #include "cell.h"
  9.  
  10. struct work { /* a unit of work is a hole-pair to connect */
  11.     int FromRow;        /* source row        */
  12.     int FromCol;        /* source column    */
  13.     char far *FromName;    /* source name        */
  14.     int ToRow;        /* target row        */
  15.     int ToCol;        /* target column    */
  16.     char far *ToName;    /* target name        */
  17.     int ApxDist;        /* approximate distance    */
  18.     int Priority;        /* 0=no, 1=yes        */
  19.     struct work far *Next;
  20.     };
  21.  
  22. /* pointers to the first and last item of work to do */
  23. static struct work far *Head = NULL;
  24. static struct work far *Tail = NULL;
  25. static struct work far *Current = NULL;
  26.  
  27. extern int SortConnects; /* 0 = don't sort, 1 = sort */
  28.  
  29. extern int GetApxDist( int, int, int, int );
  30. extern void Nomem( void );
  31.  
  32. void InitWork( void );
  33. void ReInitWork( void );
  34. void SetWork( int, int, char far *, int, int, char far *, int );
  35. void GetWork( int *, int *, char far * far *, int *, int *, char far * far * );
  36. void SortWork( void );
  37.  
  38. void InitWork () { /* initialize the work list */
  39.     struct work far *p;
  40.  
  41.     while (p = Head) {
  42.         Head = p->Next;
  43.         _ffree( p );
  44.         }
  45.     Tail = Current = NULL;
  46.     }
  47.  
  48. void ReInitWork () { /* initialize the work list */
  49.     Current = Head;
  50.     }
  51.  
  52. void SetWork ( r1, c1, n1, r2, c2, n2, pri )
  53.     /* add a unit of work to the work list */
  54.     int r1, c1, r2, c2, pri;
  55.     char far *n1;
  56.     char far *n2;
  57.     {
  58.     struct work far *p;
  59.  
  60.     if (p = (struct work far *)_fmalloc( sizeof(struct work) )) {
  61.         p->FromRow = r1;
  62.         p->FromCol = c1;
  63.         p->FromName = n1;
  64.         p->ToRow = r2;
  65.         p->ToCol = c2;
  66.         p->ToName = n2;
  67.         p->ApxDist = SortConnects ? GetApxDist( r1, c1, r2, c2 ) : 0;
  68.         p->Priority = pri;
  69.         p->Next = NULL;
  70.         if (Head) /* attach at end */
  71.             Tail->Next = p;
  72.         else /* first in list */
  73.             Head = Current = p;
  74.         Tail = p;
  75.         }
  76.     else /* can't get any more memory */
  77.         Nomem();
  78.     }
  79.  
  80. void GetWork ( r1, c1, n1, r2, c2, n2 )
  81.     /* fetch a unit of work from the work list */
  82.     int *r1, *c1, *r2, *c2;
  83.     char far * far *n1;
  84.     char far * far *n2;
  85.     {
  86.     if (Current) {
  87.         *r1 = Current->FromRow;
  88.         *c1 = Current->FromCol;
  89.         *n1 = Current->FromName;
  90.         *r2 = Current->ToRow;
  91.         *c2 = Current->ToCol;
  92.         *n2 = Current->ToName;
  93.         Current = Current->Next;
  94.         }
  95.     else { /* none left */
  96.         *r1 = *c1 = *r2 = *c2 = ILLEGAL;
  97.         *n1 = *n2 = NULL;
  98.         }
  99.     }
  100.  
  101. void SortWork () { /* order the work items; shortest first */
  102.     struct work far *p;
  103.     struct work far *q0; /* put PRIORITY CONNECTs in q0 */
  104.     struct work far *q1; /* sort other CONNECTs in q1 */
  105.     struct work far *r;
  106.  
  107.     q0 = q1 = NULL;
  108.     while (p = Head) { /* prioritize each work item */
  109.         Head = Head->Next;
  110.         if (p->Priority) { /* put at end of priority list */
  111.             p->Next = NULL;
  112.             if (!(r = q0)) /* empty list? */
  113.                 q0 = p;
  114.             else { /* attach at end */
  115.                 while (r->Next) /* search for end */
  116.                     r = r->Next;
  117.                 r->Next = p; /* attach */
  118.                 }
  119.             }
  120.         else if (!(r = q1) || p->ApxDist < q1->ApxDist) {
  121.             p->Next = q1;
  122.             q1 = p;
  123.             }
  124.         else { /* find proper position in list */
  125.             while (r->Next && p->ApxDist >= r->Next->ApxDist)
  126.                 r = r->Next;
  127.             p->Next = r->Next;
  128.             r->Next = p;
  129.             }
  130.         }
  131.     if (p = q0) { /* any priority CONNECTs? */
  132.         while (q0->Next)
  133.             q0 = q0->Next;
  134.         q0->Next = q1;
  135.         }
  136.     else
  137.         p = q1;
  138.     /* reposition Head and Tail */
  139.     for (Head = Current = Tail = p; Tail && Tail->Next; Tail = Tail->Next)
  140.         ;
  141.     }
  142.